package com.huaxiafinance.management.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.huaxiafinance.management.constant.CommonConstant;
import com.huaxiafinance.management.dto.request.QueryMessageModelDto;
import com.huaxiafinance.management.mapper.FrontSendMessageMapper;
import com.huaxiafinance.management.mapper.MessageModelMapper;
import com.huaxiafinance.management.model.FrontSendMessage;
import com.huaxiafinance.management.model.MessageModel;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.SmsSendUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 发送短信验证码
 *
 * @author yangtong
 * @create 2018-01-12 10:26
 **/
@Controller
@RequestMapping("/message")
public class MessageController {

    private static final Logger log = Logger.getLogger(MessageController.class);
    @Autowired
    private MessageModelMapper messageModelMapper;
    @Autowired
    private FrontSendMessageMapper frontSendMessageMapper;

    /**
     * 发送短信
     *
     * @param cellphone
     * @param
     * @return
     */
    @RequestMapping(value = "/sendMessage", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public String sendMessage(String cellphone, Integer id) throws Exception {
        log.info("接收短信的手机号码为-----------------:" + cellphone);
        MessageResult result = new MessageResult();
        MessageModel messageModel = messageModelMapper.beforUpdateMessageModel(id);
        String message = messageModel.getModelMessage();
        String templateCode;
        String codeTypeName;
        switch (id){
            case 36:
                templateCode = CommonConstant.LOGIN;
                break;
            case 37:
                templateCode = CommonConstant.REGISTER;
                break;
            case 38:
                templateCode = CommonConstant.MODIFY_PASSWORD;
                break;
            case 39:
                templateCode = CommonConstant.SET_PASSWORD;
                break;
            default:
                templateCode = CommonConstant.LOGIN;
                break;
        }
        Map<String, String> params = new HashMap();
        String code = SmsSendUtil.createVerificationCode();
        log.info("生成的短信验证码===================【" + code + "】手机号：" + cellphone);
        params.put("code", code);
        SendSmsResponse smsResponse = SmsSendUtil.sendSms(cellphone,templateCode,params);
        log.info("阿里云短信接口响应的数据=======================" + "responseCode="+smsResponse.getCode()+",msg="+smsResponse.getMessage());
        if(smsResponse.getCode() == null || !smsResponse.getCode().equals("OK")) {
            result.setFailureResult("获取短信验证码失败!");
            return result.getAsJSON();
        }
        String[] cellphones = cellphone.split(",");

        for (int i = 0; i < cellphones.length; i++) {
            FrontSendMessage frontSendMessage = new FrontSendMessage();
            frontSendMessage.setCellphone(cellphones[i]);
            frontSendMessage.setCode(code);
            frontSendMessage.setType(messageModel.getModelName());
            frontSendMessage.setSendAt(new Date());
            frontSendMessage.setUpdatedAt(new Date());
            frontSendMessageMapper.insertSelective(frontSendMessage);
        }
        result.setSuccessResult();
        return result.getAsJSON();
    }

    /**
     * 获取短信模板
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/getMessageModel", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getMessageModel(@ModelAttribute("dto") QueryMessageModelDto dto) {
        MessageResult result = new MessageResult();
        List<MessageModel> list = messageModelMapper.queryMessageModel(dto);
        List<MessageModel> messages = new ArrayList<>();

        for (MessageModel m : list) {
            String modelName = m.getModelName();
            if (modelName.equalsIgnoreCase("登录") || modelName.equalsIgnoreCase("注册") || modelName.equalsIgnoreCase("修改密码") || modelName.equalsIgnoreCase("设置密码")) {
                messages.add(m);
            }
        }
        result.setData(messages);
        result.setSuccessResult();
        return result.getAsJSON();
    }
}
